package com.yidu.BusinessParameters.controller;

import com.yidu.BusinessData.entity.BondEntity;
import com.yidu.BusinessData.entity.Exchange;
import com.yidu.BusinessData.service.BondService;
import com.yidu.BusinessData.service.ExchangeServce;
import com.yidu.BusinessParameters.entity.Trade;
import com.yidu.BusinessParameters.service.TradeService;
import com.yidu.Util.util.*;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 交易数据控制类
 * @author zhouhongyu
 * @since 2022/8/27
 * @version 1.0
 */
@RestController
public class TradeController {
    //自动装配
    @Autowired
    private TradeService tradeService;
    @Autowired
    ExchangeServce exchangeServce;

    //自动装配
    @Autowired
    private Dbutil dbutil;

    /**
     * 查询交易数据的控制器方法
     * @param page 起始页
     * @param limit 页码
     * @return
     */
    @RequestMapping("selectTrade")
    public Map selectTrade(String securityId,int page,String tradeDate,int limit){
        System.out.println("这是查询交易数据的控制器");
        System.out.println("securityId="+securityId);
        //遍历循环交易数据
        if(securityId == null){
            securityId="";
        }
        List<Trade> tradeList = tradeService.selectTrade(securityId,tradeDate,page,limit);
        for (Trade e:
             tradeList) {
            System.out.println(e);
        }
        int i = tradeService.selectCount();
        Map map = new HashMap<>();
        map.put("msg","");
        map.put("code",0);
        map.put("count",i);
        map.put("data",tradeList);
        return map;
    }

    /**
     * 增加数据的控制器
     * @param trade
     * @return
     */
    @RequestMapping("insertTrade")
    public String insertTrade(Trade trade, HttpSession session){
        System.out.println("这是增加交易数据的控制器");
        //调用工具类ID
        String maxId3 = dbutil.getMaxId3(TableIdUtil.DDID, TableNameUtil.T, TableTimeUtil.DDTime,trade.getTradeDate());
        trade.setTradeId(maxId3);
        String fundId = (String) session.getAttribute("fundId");
        trade.setFundId(fundId);
        System.out.println("================="+trade);
        int i = tradeService.insertTrade(trade);
        if (i == 1){
            return "增加成功";
        }else{
            return "增加失败";
        }
    }

    /**
     * 交易结算的计算方法
     * @param securitType 证券类型
     * @param exchangeName 交易所
     * @param tradePrice 单价
     * @param tradeNum 数量
     * @param tradeType
     * @return
     */
    @RequestMapping("sumTrade")
    public Trade sumTrade(String securitType,String exchangeName,int tradePrice,int tradeNum,int tradeType,String securityId){
        Trade trade = new Trade();
        Exchange exchange = new Exchange();
        exchange.setExchangeType(securitType);
        exchange.setExchangeName(exchangeName);
        int tradeMoney = tradePrice*tradeNum;
        trade.setTradeMoney(tradeMoney);
        List<Exchange> exchanges = exchangeServce.selectExchange(exchange, 1, 10);
        for (Exchange e:exchanges
             ) {
            //算出印花税
            trade.setStampDuty(e.getStampDuty());
            //算出过户费
            trade.setTransferFee(e.getTransferFee());
            //算出征管费
            trade.setCollectionFee(e.getCollectionFee());
            //算出经手费
            trade.setHandlingFee(e.getHandlingFee());
        }
        trade.setFees(0);
        trade.setBoodInterest(2*tradeMoney/100/365);
        if(tradeType==1){ //1为买入
            //算出结算金额
            trade.setSettleMoney(tradeMoney+trade.getStampDuty()+trade.getTransferFee()+trade.getCollectionFee()+trade.getHandlingFee()+trade.getFees());
        }else {
            //算出结算金额
            trade.setSettleMoney(tradeMoney-trade.getStampDuty()-trade.getTransferFee()-trade.getCollectionFee()-trade.getHandlingFee()-trade.getFees());
        }
        return trade;
    }

    /**
     * 修改交易数据的控制器
     * @param trade
     * @return
     */
    @RequestMapping("updateTrade")
    public String updateTrade(Trade trade){
        System.out.println("这是修改交易数据的控制器");
        int i = tradeService.updateTrade(trade);
        System.out.println(trade);
        if (i == 1){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }

    /**
     * 删除交易数据的控制器
     * @param tradeId
     * @return
     */
    @RequestMapping("deleteTrade")
    public String deleteTrade(String tradeId){
        System.out.println("这是删除交易数据的控制器");
        int i = tradeService.deleteTrade(tradeId);
        if (i == 1){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }


    //查询已结算
    @RequestMapping("selectSettlement")
    public Map selectSettlement(String tradeType,String tradeDate,int page,int limit){
        System.out.println("这是交易结算中查询已结算的方法,方法名为：selectSettleState");
        System.out.println("tradeDate"+tradeDate);
        System.out.println(tradeType);
        if(tradeDate ==null){
            tradeDate="";
        }
        if(tradeType==null){
            tradeType="";
        }
        Map map = new HashMap<>();
        List<Trade> tradeList = tradeService.selectSettlement(tradeType,tradeDate,page,limit);
        int  i = tradeService.selectCount();
        map.put("code","0");
        map.put("msg","");
        map.put("count",i);
        map.put("data",tradeList);
        return map;
    }

    //查询未结算
    @RequestMapping("selectSettlement2")
    public Map selectSettlement2(String tradeType, String tradeDate, int page, int limit){
        System.out.println("这是交易结算中查询未结算的方法,方法名为：selectSettleState2");
        System.out.println("tradeDate"+tradeDate);
        if(tradeDate ==null){
            tradeDate="";
        }
        if(tradeType==null){
            tradeType="";
        }

        System.out.println(tradeType);
        Map maps = new HashMap<>();
        List<Trade> tradeList = tradeService.selectSettlement2(tradeType,tradeDate,page,limit);
        int i = tradeService.selectCount();
        maps.put("code","0");
        maps.put("msg","");
        maps.put("count",i);
        maps.put("data",tradeList);
        return maps;
    }

    /**
     * 修改未结算控制器
     * @param
     * @return
     */
    @RequestMapping("/updateSettlement")
    public int updateSettlement(String trade){

        System.out.println("这是修改未结算的方法，方法名为+updateSettlement");
        System.out.println(trade);
        int i = tradeService.updateSettlement(trade);
        System.out.println("i="+i);

        return i;
    }

    /**
     * 修改已结算控制器
     * @param
     * @return
     */
    @RequestMapping("/updateSettled")
    public int updateSettled(String jsonTradeList){
        //把json格式list转为list
        List<Trade> tradeList = SysUtil.jsonToArrayList(jsonTradeList, Trade.class);
        int i = 0;
        int y=0;
        for (Trade trade:tradeList){
            //删除资金调拨对应的数据
            i = tradeService.deleteMoneyAllocate(trade.getTradeId());
            if (i==1){
                //已结算修改成未结算
                y += tradeService.updateSettled(trade.getTradeId());
            }
        }

        return y;
    }
}
